package com.xjh.basestudy.javabase.io.byteio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//hello
//java
//xujianhua
public class FileInputStream2 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("javabase\\fis.txt");
/*
        byte[] bys = new byte[5];//定义一个可以存储5个字符的字符数组
        //每次换行都会读到\r\n算两个字符
        //hello
        int len = fis.read(bys);//用len代表读到的字符个数
        System.out.println(len);//输出读到的字符个数
        //String(byte[] bytes)直接将一个字节数组变为一个字符串
        System.out.println(new String(bys,0,len));;//转换为字符串输出读到的内容,加参数是为了只将实际读取到的长度转换为字符串
        //\r\njav
        len = fis.read(bys);
        System.out.println(len);
        System.out.println(new String(bys,0,len));;
        // DruidDemo\r\nxu
        len = fis.read(bys);
        System.out.println(len);
        System.out.println(new String(bys,0,len));
 */
        //改进：用循环解决问题
        byte[] bys = new byte[1024];//1024及其整数倍
        int len;
        while((len = fis.read(bys))!=-1){
            System.out.println(new String(bys,0,len));
        }
        fis.close();
    }
}
